class LostItemsController < ApplicationController
  # GET /lost_items
  # GET /lost_items.json
  def index
    @user = User.find_by_id(session[:user_id])
    
    session[:show_resolved_lost_items] ||= false
    @show_resolved = session[:show_resolved_lost_items]
	
    if @show_resolved
      @lost_items = LostItem.find(:all, :order => "created_at DESC")
    else
      @lost_items = LostItem.find(:all, :conditions => [ "resolved = ?", false], :order => "created_at DESC")
    end
    #@lost_items = LostItem.find(:all, :order => "created_at DESC")
    
    if @user.permission_level != "admin"
      @lost_items = @lost_items.find_all{|lost_item|
        lost_item.item && 
        ((lost_item.item.source_id == @user.location.id) ||
        (lost_item.item.destination_id == @user.location.id))
      }
    end

    @page_results = @lost_items.paginate(:page => params[:page], :per_page => 10)

    @title = "Lost Items"
    
    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @lost_items }
    end
  end

  # GET /lost_items/new
  # GET /lost_items/new.json
  def new
    @lost_item = LostItem.new
    @identifier = Identifier.new
    
    @title = "New Lost Item"
    
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @lost_item }
    end
  end

  # GET /lost_items/1/edit
  def edit
    @lost_item = LostItem.find(params[:id])
    if @lost_item.item && @lost_item.item.identifiers
		@identifier = @lost_item.item.identifiers.first
    else
		@identifier = Identifier.new
    end
	@title = "Edit Lost Item"
  end

  # POST /lost_items
  # POST /lost_items.json
  def create
    if request.content_type == "application/json"
      @lost_item = LostItem.create(
        :item => Item.find_by_id(params["item_id"]),
        :comment => params["comment"]
      )
    else 
      @identifier = Identifier.find_by_identifier_value(params[:identifier][:identifier_value])
      if @identifier.nil?
        flash_msg = "Identifier \"#{params[:identifier][:identifier_value]}\" not found"
      else
        params[:lost_item][:item] = @identifier.item
        @lost_item = LostItem.new(params[:lost_item])
      end
    end

    respond_to do |format|
      if @lost_item
        if @lost_item.save
          flash[:notice] = 'Lost Item was successfully created.'
          format.html { redirect_to lost_items_path }
          format.json { render :json => @lost_item, :status => :created, :location => @lost_item }
        else
          flash[:notice] = 'That Item has already been marked as lost.'
          format.html { redirect_to lost_items_path }
          format.json { render :json => @lost_item.errors, :status => :unprocessable_entity }
        end
      else
        flash[:notice] = flash_msg
        @lost_item = LostItem.new
        @identifier = Identifier.new
        format.html { render 'new' }
        format.json { render :json => @lost_item.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /lost_items/1
  # PUT /lost_items/1.json
  def update
    @lost_item = LostItem.find(params[:id])

    respond_to do |format|
      if @lost_item.update_attributes(params[:lost_item])
        flash[:notice] = 'Lost Item was successfully updated.'
        format.html { redirect_to lost_items_path }
        format.json { head :ok }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @lost_item.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /lost_items/1
  # DELETE /lost_items/1.json
  def destroy
    @lost_item = LostItem.find(params[:id])
    @lost_item.destroy

    respond_to do |format|
      format.html { redirect_to(lost_items_url) }
      format.json { head :ok }
    end
  end
  
  def toggle_resolved
    session[:show_resolved_lost_items] ||= false
    session[:show_resolved_lost_items] = !session[:show_resolved_lost_items]
	
    redirect_to :back
  end
end
